/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.sd.service;

import java.util.List;

import com.jeesite.common.config.Global;
import com.jeesite.modules.sd.common.constants.DestoyType;
import com.jeesite.modules.sd.entity.DocDeliverRecord;
import com.jeesite.modules.sd.entity.DocDestoyRecord;
import org.apache.commons.compress.archivers.sevenz.SevenZOutputFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.sd.entity.DocDestroyTask;
import com.jeesite.modules.sd.dao.DocDestroyTaskDao;

import javax.print.Doc;

/**
 * 文件销毁任务Service
 * @author ysj
 * @version 2020-08-07
 */
@Service
@Transactional(readOnly=true)
public class DocDestroyTaskService extends CrudService<DocDestroyTaskDao, DocDestroyTask> {

	@Autowired
	private DocDeliverRecordService recordService;

	@Autowired
	private DocDestoyRecordService destoyRecordService;
	/**
	 * 获取单条数据
	 * @param docDestroyTask
	 * @return
	 */
	@Override
	public DocDestroyTask get(DocDestroyTask docDestroyTask) {
		return super.get(docDestroyTask);
	}
	
	/**
	 * 查询分页数据
	 * @param docDestroyTask 查询条件
	 * @return
	 */
	@Override
	public Page<DocDestroyTask> findPage(DocDestroyTask docDestroyTask) {
		return super.findPage(docDestroyTask);
	}
	
	/**
	 * 保存数据（插入或更新）
	 * @param docDestroyTask
	 */
	@Override
	@Transactional(readOnly=false)
	public void save(DocDestroyTask docDestroyTask) {
		super.save(docDestroyTask);
	}
	
	/**
	 * 更新状态
	 * @param docDestroyTask
	 */
	@Override
	@Transactional(readOnly=false)
	public void updateStatus(DocDestroyTask docDestroyTask) {
		super.updateStatus(docDestroyTask);
	}
	
	/**
	 * 删除数据
	 * @param docDestroyTask
	 */
	@Override
	@Transactional(readOnly=false)
	public void delete(DocDestroyTask docDestroyTask) {
		super.delete(docDestroyTask);
	}

	/**
	 * 修改销毁任务文件状态 并添加销毁文件
	 *
	 */
	@Transactional(readOnly=false,rollbackFor = Exception.class)
	public void addDestotyFile(String[] ids,String destroyId) {
		if (ids.length==0){
			throw new RuntimeException("没有选择要销毁的文件");
		}
		//修改销毁任务状态
		DocDestroyTask docDestroyTask=new DocDestroyTask();
		docDestroyTask.setId(destroyId);
		docDestroyTask.setDestoyType(DestoyType.DESTOY_TYPE_UPDATE.getValue());
		this.update(docDestroyTask);
		for (int i=0;i<ids.length;i++){
			DocDeliverRecord docDeliverRecord = recordService.get(ids[i]);
			DocDestoyRecord destoyRecord=new DocDestoyRecord();
			destoyRecord.setDestoyId(destroyId);
			destoyRecord.setDeliverId(ids[i]);
			destoyRecord.setRecvId(docDeliverRecord.getDocId());
			destoyRecordService.save(destoyRecord);

			 docDeliverRecord.setDestroyTaskId(destroyId);
			 recordService.update(docDeliverRecord);
		}
	}
	
}